import ConfigMarks from "../config-marks.js";
import ToolData from "../tool/tool-data.js";
import FactoryPlane from "../factory/factory-plane.js";
import FactoryPoint from "../factory/factory-point.js";

let layer;
let map;
let layerAreaBorder;
let geoJsonLayer;
let layerAreaBorderTag = false;
let layerUis;

const layerArea = {
	init(mapParam) {
		map = mapParam;

		// layer = new mars3d.layer.GraphicLayer({
		// 	name: "区域样式图层",
		// 	zIndex: 999
		// });

		ToolData.queryChinaMapFullBorderData().then(data => {
			setTimeout(() => {
				map.addLayer(layerAreaBorder = FactoryPlane.areaBorder(data));
			}, 1000);
		});
			
		let rectangle = {
			xmin: 71.602355,
			xmax: 136.815065,
			ymin: 13.811691,
			ymax: 54.363269
		};
			
		let rectangleBak = {
			xmin: 51.602355,
			xmax: 156.815065,
			ymin: -7.811691,
			ymax: 74.363269
		};
		
		layerUis = [
			FactoryPlane.planeLayerImg(rectangleBak, "/admin/static/img/map/map-all-bak.png"),
			FactoryPlane.planeLayerImg(rectangle, "/admin/static/img/map/map-bak-01.png"),
			FactoryPlane.planeLayerImg(rectangle, "/admin/static/img/map/map-bak-02.png"),
			FactoryPlane.planeLayerImg(rectangle, "/admin/static/img/map/map-bak-03.png"),
			FactoryPlane.planeLayerImg(rectangle, "/admin/static/img/map/map-bak-04.png"),
			FactoryPlane.planeLayerImg(rectangle, "/admin/static/img/map/map-bak-05.png"),
			FactoryPlane.planeLayerImg(rectangle, "/admin/static/img/map/map-bak-06.png"),
		]
		
		for (let i = 0; i < layerUis.length; i++) {
			setTimeout(() => {
				map.addLayer(layerUis[i]);
			}, i * 100);
		}
	
		// map.addLayer(layer);
	},
	hide() {
		for (var i = 0; i < layerUis.length; i++) {
			layerUis[i].setOptions({ show: false })
		}
		layerAreaBorder.setOptions({ show: false })
	},
	show() {
		for (var i = 0; i < layerUis.length; i++) {
			layerUis[i].setOptions({ show: true })
		}
		layerAreaBorder.setOptions({ show: true })
	},
	add(entity) {
		layer.addGraphic(entity);
	},
	clear() {
		layer.clear();
	},
	async initArea(areaCode = "100000", style = FactoryPolygons.polygons101.style(), positions) {

		if (!positions) {
			let data = await ToolData.queryChinaMapBorderData(areaCode);
			positions = data.features[0].geometry.coordinates[0][0].map(item => ({
				lat: item[1],
				lng: item[0],
				alt: 99990000
			}));
		}

		layer.clear();

	}
}

export default layerArea;
